package com.core.ingenium.view.epadilla;


import java.io.FileNotFoundException;

import java.util.List;

import javax.faces.application.ViewHandler;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.view.rich.component.rich.input.RichInputFile;

import org.apache.myfaces.trinidad.model.UploadedFile;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class IndexController{
    
    private UploadedFile estudianteFile;
    private UploadedFile carreraFile;
    private UIComponent inputFile;
    private List<EstudianteExcel> lstEstudiante;
    private List<CarreraExcel> lstCarrera;
    private String msj;
    
    public IndexController() {
        this.setMsj("");    
    }
    
    public String updateEstudiante(ValueChangeEvent valueChangeEvent) {
        
        RichInputFile inputFileComponent = (RichInputFile)valueChangeEvent.getComponent();
        UploadedFile newFile = (UploadedFile)valueChangeEvent.getNewValue();
        UploadedFile oldFile = (UploadedFile)valueChangeEvent.getOldValue();
        
        if(newFile.getFilename().endsWith(".xls")){
            this.setMsj("");
            POIFSFileSystem fs;
            try {
                fs = new POIFSFileSystem(newFile.getInputStream());
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                HSSFSheet sheet =wb.getSheet("lista");
                RowProcessor ip = ExcelProcessor.getInstance();
                this.setLstEstudiante(ip.processEstudiante(sheet));
                
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
            }
        }else{
            this.setMsj("El archivo excel debe ser formato .xlx");
        }
        
       
        refrescar();
        return null;
    }
    
    public String updateCarrera(ValueChangeEvent valueChangeEvent) {
        RichInputFile inputFileComponent = (RichInputFile)valueChangeEvent.getComponent();
        UploadedFile newFile = (UploadedFile)valueChangeEvent.getNewValue();
        UploadedFile oldFile = (UploadedFile)valueChangeEvent.getOldValue();
        
        if(newFile.getFilename().endsWith(".xls")){
            this.setMsj("");
            POIFSFileSystem fs;
            try {
                fs = new POIFSFileSystem(newFile.getInputStream());
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                HSSFSheet sheet =wb.getSheet("lista");
                RowProcessor ip = ExcelProcessor.getInstance();
                this.setLstCarrera(ip.processCarrera(sheet));
                
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
            }
        }else{
            this.setMsj("El archivo excel debe ser formato .xlx");
        }
        refrescar();
        return null;
    }
    
    private String refrescar(){
         FacesContext fc = FacesContext.getCurrentInstance();
         String refreshpage = fc.getViewRoot().getViewId();
         ViewHandler ViewH = fc.getApplication().getViewHandler();
         UIViewRoot UIV = ViewH.createView(fc, refreshpage);
         UIV.setViewId(refreshpage);
         fc.setViewRoot(UIV);
         return null;
    }
    
    //setters
    public void setInputFile(UIComponent inputFile) {this.inputFile = inputFile;}
    public void setLstEstudiante(List<EstudianteExcel> lstEstudiante) {this.lstEstudiante = lstEstudiante;}
    public void setEstudianteFile(UploadedFile estudianteFile) {this.estudianteFile = estudianteFile;}
    public void setCarreraFile(UploadedFile carreraFile) {this.carreraFile = carreraFile;}
    public void setLstCarrera(List<CarreraExcel> lstCarrera) {this.lstCarrera = lstCarrera;}
    public void setMsj(String msj) {this.msj = msj;}

    //getters
    public UIComponent getInputFile() {return inputFile;}
    public List<EstudianteExcel> getLstEstudiante() {return lstEstudiante;}
    public UploadedFile getEstudianteFile() {return estudianteFile;}
    public UploadedFile getCarreraFile() {return carreraFile;}
    public List<CarreraExcel> getLstCarrera() {return lstCarrera;}
    public String getMsj() {return msj;}
}
